import {ARTICLE_ADDRESS, IMG_ADDRESS} from "./request"


export function changeImgSrc(tems,dir) {
    tems = tems.split(dir);
    tems[0] = "";
    tems = tems.join(dir);
    tems = tems.replaceAll(/\\/g,"/");
    tems = ARTICLE_ADDRESS+tems
    return tems;
}


export function newChangeImgSrc(prefix,dir,noteFileName,tems) {
    if(tems.match("http"))return tems;
    let noteName = noteFileName.split(".")[0];
    let s = tems.split("\\");
    let fileName = s[s.length-1];
    dir = dir ? `/${dir}` : "";
    let ans = IMG_ADDRESS + `${prefix}${dir}/${noteName}/${fileName}`;
    // console.log(ans);
    return ans;
}


export function fileUrl(type,dir,file) {
    switch (type) {
        case "article":
            return `/${type}/${dir}/${file}`;
        case "movie":
            return `/${type}/${file}`;
        case "essay":
            return `/${type}/${file}`;
    }
}

/*
    参数传递：
        1.第一个参数为选取移动的目标
        2.第二个参数为移动的目标--可以省略，省略了那么就是第一个
*/
export function initDrag(picker,mover) {
    let pickerBox =  document.querySelector(picker);
    let box = document.querySelector(picker);
    if(mover) {
        box = document.querySelector(mover);
    }
    var mouseDown = false
    var mouseX,mouseY
    box.style.transform = "translate(0px,0px)"
    pickerBox.onmousedown = function(ev) {
        mouseDown = true
        mouseX = ev.clientX
        mouseY = ev.clientY
    }
    pickerBox.onmouseup = function(ev) {
        mouseDown = false
    }
    
    document.addEventListener('mousemove',(ev)=>{
        if(!mouseDown)return 
        // 获取之前的位置
        let s = box.style.transform
        let t1 = s.indexOf('(',0)
        let t2 = s.indexOf('px',0)
        let posX = parseInt(s.slice(t1+1,t2))
        t1 = s.indexOf(',',0)
        t2 = s.indexOf("px",t2+2)
        let posY = parseInt(s.slice(t1+1,t2))

        box.style.transform = `translate(${ev.clientX-mouseX+posX}px,${ev.clientY-mouseY+posY}px)`
        mouseX = ev.clientX
        mouseY = ev.clientY
    })
}